package com.cskaoyan.wordmemorize.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cskaoyan.wordmemorize.aop_whh.annotation.OperateLog;
import com.cskaoyan.wordmemorize.auth.StpKit;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dao.entity.PermissionDO;
import com.cskaoyan.wordmemorize.dao.mapper.PermissionMapper;
import com.cskaoyan.wordmemorize.dto.admin.DeleteEmployeeRoleOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.EmployeeDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.request.EmployeeCommand;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.service.EmployeeService;
import com.cskaoyan.wordmemorize.service.FileUploadService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("admin/employee")
public class EmployeeController {


    @Autowired
    PermissionMapper permissionMapper;

    @Resource
    FileUploadService fileUploadService;

    @Resource
    EmployeeService employeeService;

    // 获取用户信息
    @GetMapping("info")
    public Result<EmployeeDTO> getUserInfo() {
        //获取ID
        long userId = StpKit.ADMIN.getLoginIdAsLong();
        EmployeeDTO employeeDTO = employeeService.getEmployeeInfo(userId);
        return Result.ok(employeeDTO);
    }
    //获取后台员工列表（分页查询）
    @GetMapping("list")
    public Result<PageDTO<EmployeeDTO>> getEmployeeList(PageRequest pageRequest) {
        PageDTO<EmployeeDTO> employeePage = employeeService.getEmployeePage(pageRequest);
        return Result.ok(employeePage);
    }

    //后台添加员工
    @OperateLog(dateType = "员工",operateType = 1)
    @PostMapping()
    public Result<String> addEmployee(@RequestBody EmployeeCommand employeeDTO) {
        employeeService.addEmployee(employeeDTO);
        return Result.ok();
    }

    //后台修改员工信息
    @PutMapping()
    @OperateLog(dateType = "员工",operateType = 4)
    public Result<String> updateEmployee(@RequestBody EmployeeCommand employeeDO) {
        employeeService.updateEmployee(employeeDO);
        return Result.ok();
    }

    //上传头像
    @PostMapping("avatar")
    // @OperateLog(dateType = "员工",operateType = 1)
    public Result<String> uploadAvatar(MultipartFile file) throws IOException {
        String upload = fileUploadService.upload(file);
        return Result.ok(upload);
    }
    //后台删除员工信息
    @OperateLog(dateType = "员工",operateType = 2)
    @DeleteMapping("{employeeId}")
    public Result<String> deleteEmployee(@PathVariable("employeeId") Long employeeId) {
        employeeService.delete(employeeId);
        return Result.ok();
    }

    //后台删除员工-角色关联关系
    @OperateLog(dateType = "员工",operateType = 2)
    @DeleteMapping("{employeeId}/role/{roledId}")
    public Result<String> deleteEmployeeRole(@PathVariable("employeeId") Long employeeId,
                                             @PathVariable("roledId") Long roledId) {
        employeeService.deleteRole(employeeId, roledId);
        return Result.ok();
    }


}
